<template>
  <van-icon
  :name="value ? 'good-job' : 'good-job-o'"
  :color="value === 1 ? '#ff0000' : '#777'"
  :loading="loading"
  @click="onCollect"
  />
</template>

<script>
import { addLikeAPI, deleteLikeAPI } from '../api/index.js'
export default {
  name: 'LikeArticle',
  props: {
    value: {
      type: Number,
      default: -1
    },
    articleId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async onCollect () {
      this.loading = true
      try {
        let status = -1
        if (this.value === 1) {
          // 已点赞，取消点赞
          await deleteLikeAPI(this.articleId)
        } else {
          // 没有点赞，添加点赞
          await addLikeAPI(this.articleId)
          status = 1
        }

        // 更新视图
        this.$emit('input', status)

        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast.fail('操作失败，请重试！')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less">
.collected {
  .van-icon {
    color: red;
  }
}
</style>
